package com.asiainfo.kbisheet.components.condition.month
{
	import com.asiainfo.kbisheet.common.Util;
	import com.asiainfo.kbisheet.components.condition.ConditionItem;
	import com.asiainfo.kbisheet.event.GetDateDimResultEvent;
	import com.asiainfo.kbisheet.model.SettingModel;
	import com.asiainfo.kbisheet.vo.DimVO;
	import com.asiainfo.kbisheet.vo.WhereVO;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * 月条件的ConditionItem扩展。
	 */
	public class ConditionItemMonth extends ConditionItem
	{
		public function ConditionItemMonth(xml:XML)
		{
			super(xml);
		}
		
		
		/** 
		 * 初始化日期控件数据
		 */
		override public function initData():void{
			/*创建IKbsControl的实例*/
			if(kbsControl == null){
				kbsControl = SettingModel.getInstance().getKbsControl();
				kbsControl.addEventListener("getDateDimResult",getDateDimResult);
			}
			
			var dimVO:DimVO = new DimVO();
			dimVO.sourceKey = kbsModel.kbsSheetVO.sourceKey;
			//dimVO.table = conditionXML.dim.@table;
			dimVO.table = Util.getModeAttribute(conditionXML.dim, 'table');
			if(dimVO.table == '*'){
				/*没有维表，从自身的列中筛选*/
				dimVO.self = true;
				dimVO.table = kbsModel.kbsSheetVO.tableName;
				dimVO.column = conditionXML.column;
			}
			else{
				dimVO.self = false;
				dimVO.from = conditionXML.dim.@from;
				dimVO.to = conditionXML.dim.@to;
				
				var wheres:ArrayCollection = new ArrayCollection();
				var where:WhereVO = new WhereVO();
				where.column =  conditionXML.dim.@tableName;
				where.equalSign = "=";
				if(conditionXML.dim.hasOwnProperty('@tableNameValue')){
					where.value = conditionXML.dim.@tableNameValue;
				}
				else{
					var tableNameLink:String = conditionXML.dim.@tableNameLink;
					if(tableNameLink == "d.t"){
						where.value = kbsModel.kbsSheetVO.databaseName+"."+kbsModel.kbsSheetVO.tableName;
					}
					else{
						where.value = kbsModel.kbsSheetVO.tableName;
					}
				}
				wheres.addItem(where);
				
				dimVO.wheres = wheres;
				dimVO.columns = kbsModel.columns;
			}
			
			kbsControl.getDimDate(dimVO);
		}
		
		private function getDateDimResult(event:GetDateDimResultEvent):void{
			var value:Object = event.value;
			for each(var conditionInput:ConditionInputMonth in conditionInputHandler){
				conditionInput.selectableRange = value;				
			}
			dispatchUpdateConditionComplete();
		}
	}
}